<?php
namespace Admin\Controller;
class HouseTypeController extends CommonController{
    public function _initialize() {
		parent::_initialize();
        $this->_mod = D('HouseType');
        $menu = getLinkMenuFor(18,1);
        $this->saleStatus = $menu[18];
        $this->house_id = intval($_GET['house_id']);
        $this->url = '&house_id='.$this->house_id;
    }

    public function _before_index() {
        $p = I('p',1,'intval');
        $this->house = M('house')->where(array('id'=>$this->house_id))->field('title,id')->find();
        $this->assign('p',$p);
    }

    protected function _search() {
        $map = array();
        $map['house_id'] = $this->house_id;
        return $map;
    }

    public function _before_add(){
        $id = I('get.house_id',0,'int');
        if($id){
            $this->info = M('house')->where(array('id'=>$id))->field('title,id')->find();
            $this->property = M('property')->where(array('house_id'=>$id))->order('id asc')->select();
        }
    }

    protected function _before_insert($data) {
        $data['add_time']   = time();
        $data = $this->_updatePic($data);
        if(isset($_POST['pic'])){
            $data['img'] = $_POST['pic']['pic'][0];
        }
        return $data;
    }


    //增加成功返回
    protected function _after_insert($id){
        if(count($_POST['property'])){
            foreach($_POST['property'] as $v){
                $property = M('property')->where(array('id'=>$v))->find();
                if($property['house_type']){
                    $house_type = $property['house_type'].','.$id;
                }else{
                    $house_type = $id;
                }
                M('property')->where(array('id'=>$v))->save(array('house_type'=>$house_type));
            }
        }
    }

    public function _before_edit(){
        $id = I('get.id');
        if($id){
            $type = $this->_mod->field('id,house_id,file')->where(array('id'=>$id))->find();
            $house = M('house')->where('id='.$type['house_id'])->field('id,title')->find();
            $this->property = M('property')->where(array('house_id'=>$house['id']))->order('id asc')->select();
            $this->assign('house',$house);
            $this->assign('file',json_decode($type['file'],true));
        }
    }

    protected function _before_update($data){
        $data = $this->_updatePic($data);
        if(isset($_POST['pic'])){
            $data['img'] = $_POST['pic']['pic'][0];
        }
       return $data;
    }

    protected function _after_update($id){
        $houseType = M('HouseType')->where(array('id'=>$id))->find();
        //楼栋
        $property = M('property')->where(array('house_id'=>$houseType['house_id']))->select();
        foreach($property as $v){
            if($v['house_type']){
                $type = explode(',',$v['house_type']);
                $newType = array_flip($type);
                unset($newType[0]);
                unset($newType[$id]);  //先在所有的楼栋中删除户型
                $newType = array_flip($newType);
            }else{
                $newType = array();
            }
            if(count($_POST['property'])){
                foreach($_POST['property'] as $val){
                    if($val==$v['id']){
                        $newType[] = $id;
                    }
                }
            }
            $house_type = implode(',',$newType);
            M('property')->where(array('id'=>$v['id']))->save(array('house_type'=>$house_type));
            unset($newType);
        }
    }


    public function ajaxCheckTitle(){
        $title = urldecode(I('get.J_title',''));
        if($title){
            $info = $this->_mod->where(array('name'=>$title))->find();
            if($info){
                $this->ajaxReturn(0);
            }else{
                $this->ajaxReturn(1);
            }
        }else{
            $this->ajaxReturn(1);
        }
    }

}